/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package OtrosTPs.TP2;

import OtrosTPs.TP1.LevenshteinDistance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author ivanpianetti
 */
public class ArbolBKT extends javax.swing.JFrame {

    private static int nivelesPermitidos = 0;
    private static int alturaActual = 0;
    private DefaultMutableTreeNode nodoRaiz;
    private DefaultComboBoxModel modelListaCadenas;
    private DefaultComboBoxModel modelListaRespuestas;
    private String consulta;
    private int cantidadCalculos = 0;
    private ArrayList<String> listaNombre = new ArrayList<String>();
    private ArrayList<String> ListaCadenas = new ArrayList<String>();

    /**
     * Creates new form ArbolBKT
     */
    public ArbolBKT() {
        initComponents();
        ListaCadenas = new ArrayList<String>();
        ListaCadenas.add("Paula");
        ListaCadenas.add("Ana");
        ListaCadenas.add("Alan");
        ListaCadenas.add("Amy");
        ListaCadenas.add("Adrian");
        ListaCadenas.add("Anahi");
        ListaCadenas.add("Anama");
        ListaCadenas.add("Belen");
        ListaCadenas.add("Cameron");
        ListaCadenas.add("Charlize");
        ListaCadenas.add("Claudia");
        ListaCadenas.add("Clara");
        ListaCadenas.add("Cristian");
        ListaCadenas.add("Esteban");
        ListaCadenas.add("Estanislao");
        ListaCadenas.add("Fernando");
        ListaCadenas.add("Francisco");
        ListaCadenas.add("Jorge");
        ListaCadenas.add("Pedro");
        ListaCadenas.add("Zulma");

        //////////////////////
//        modelListaCadenas = new DefaultComboBoxModel(ListaCadenas.toArray());
//        jListCadenas.setModel(modelListaCadenas);
        jTextFieldNivelesPermitidos.setText("2");
        jTextFieldTamBucket.setText("2");
        jTextFieldConsulta.setText("Roberto");
        jTextFieldRadio.setText("5");
        //////////////////////


        nodoRaiz = crearArbol(ListaCadenas);
        DefaultTreeModel modeloArbol = new DefaultTreeModel(nodoRaiz);
        jTreeArbol.setModel(modeloArbol);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrameSelectRaiz = new javax.swing.JFrame();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListCadenas = new javax.swing.JList();
        jButtonSetRaiz = new javax.swing.JButton();
        buttonGroupTipoArbol = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTreeArbol = new javax.swing.JTree();
        jButtonCalcularDistancia = new javax.swing.JButton();
        jButtonReset = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldConsulta = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldRadio = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldCantidadDeCalculos = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListListaRespuestas = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxTipoBusqueda = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldNivelesPermitidos = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jRadioButtonBKT = new javax.swing.JRadioButton();
        jRadioButtonFQT = new javax.swing.JRadioButton();
        jRadioButtonFHQT = new javax.swing.JRadioButton();
        jRadioButtonFQA = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldTamBucket = new javax.swing.JTextField();
        jLabelCantResp = new javax.swing.JLabel();
        jCheckBoxRandom = new javax.swing.JCheckBox();

        jLabel2.setText("Seleccione un nodo raíz:");

        jListCadenas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jListCadenas);

        jButtonSetRaiz.setText("Set Raiz");
        jButtonSetRaiz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSetRaizActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jFrameSelectRaizLayout = new org.jdesktop.layout.GroupLayout(jFrameSelectRaiz.getContentPane());
        jFrameSelectRaiz.getContentPane().setLayout(jFrameSelectRaizLayout);
        jFrameSelectRaizLayout.setHorizontalGroup(
            jFrameSelectRaizLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameSelectRaizLayout.createSequentialGroup()
                .add(38, 38, 38)
                .add(jFrameSelectRaizLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jFrameSelectRaizLayout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(42, 42, 42)
                        .add(jButtonSetRaiz))
                    .add(jLabel2))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jFrameSelectRaizLayout.setVerticalGroup(
            jFrameSelectRaizLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameSelectRaizLayout.createSequentialGroup()
                .add(jFrameSelectRaizLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jFrameSelectRaizLayout.createSequentialGroup()
                        .add(28, 28, 28)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 184, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jFrameSelectRaizLayout.createSequentialGroup()
                        .add(81, 81, 81)
                        .add(jButtonSetRaiz)))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTreeArbol.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTreeArbol.setVisibleRowCount(50);
        jScrollPane1.setViewportView(jTreeArbol);

        jButtonCalcularDistancia.setText("Calcular Distancia");
        jButtonCalcularDistancia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCalcularDistanciaActionPerformed(evt);
            }
        });

        jButtonReset.setText("Reset");
        jButtonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResetActionPerformed(evt);
            }
        });

        jLabel1.setText("Consulta:");

        jLabel3.setText("Rango:");

        jLabel4.setText("Calculos:");

        jScrollPane3.setViewportView(jListListaRespuestas);

        jLabel5.setText("Respuestas:");

        jComboBoxTipoBusqueda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Rango", "NN", "NNk", " " }));
        jComboBoxTipoBusqueda.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxTipoBusquedaItemStateChanged(evt);
            }
        });

        jLabel7.setText("Niveles:");

        jLabel8.setText("Seleccione un tipo de árbol:");

        buttonGroupTipoArbol.add(jRadioButtonBKT);
        jRadioButtonBKT.setSelected(true);
        jRadioButtonBKT.setText("BKT");

        buttonGroupTipoArbol.add(jRadioButtonFQT);
        jRadioButtonFQT.setText("FQT");

        buttonGroupTipoArbol.add(jRadioButtonFHQT);
        jRadioButtonFHQT.setText("FHQT");

        buttonGroupTipoArbol.add(jRadioButtonFQA);
        jRadioButtonFQA.setText("FQA");

        jLabel6.setText("Bucket:");

        jCheckBoxRandom.setText("Random?");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(27, 27, 27)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel8)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jRadioButtonBKT)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jRadioButtonFQT)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jRadioButtonFHQT)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jRadioButtonFQA))
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(29, 29, 29)
                                .add(jButtonCalcularDistancia))
                            .add(layout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel5)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jLabelCantResp))
                                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                                .add(jButtonReset)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(jCheckBoxRandom))
                                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                                .add(6, 6, 6)
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                    .add(jLabel6)
                                                    .add(jLabel7))
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                    .add(jTextFieldNivelesPermitidos)
                                                    .add(jTextFieldTamBucket, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                            .add(layout.createSequentialGroup()
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                    .add(jLabel1)
                                                    .add(jLabel3)
                                                    .add(jLabel4))
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                    .add(jTextFieldConsulta)
                                                    .add(jTextFieldRadio)
                                                    .add(jTextFieldCantidadDeCalculos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jComboBoxTipoBusqueda, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jRadioButtonBKT)
                            .add(jRadioButtonFQT)
                            .add(jRadioButtonFHQT)
                            .add(jRadioButtonFQA))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 391, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(33, 33, 33)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonReset)
                            .add(jCheckBoxRandom))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(jTextFieldNivelesPermitidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel6)
                            .add(jTextFieldTamBucket, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(jButtonCalcularDistancia)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jTextFieldConsulta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jTextFieldRadio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3)
                            .add(jComboBoxTipoBusqueda, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(jTextFieldCantidadDeCalculos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel5)
                            .add(jLabelCantResp))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 111, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCalcularDistanciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCalcularDistanciaActionPerformed
        consulta = jTextFieldConsulta.getText().toString();
        modelListaRespuestas = new DefaultComboBoxModel();
        listaNombre = new ArrayList<String>();
        cantidadCalculos = 0;
        if (jComboBoxTipoBusqueda.getSelectedItem() == "Rango") {
            calcDistanciaSegunRadio(nodoRaiz);
        }
        if (jComboBoxTipoBusqueda.getSelectedItem() == "NN") {
            int i = 0;
            while (listaNombre.isEmpty()) {
                jTextFieldRadio.setText(i + "");
                calcDistanciaSegunRadio(nodoRaiz);
                i++;
            }
        }
        if (jComboBoxTipoBusqueda.getSelectedItem() == "NNk") {
            int cant = Integer.parseInt(jTextFieldRadio.getText().toString());
            int i = 0;
            while (listaNombre.size() < cant) {
                jTextFieldRadio.setText(i + "");
                listaNombre = new ArrayList<String>();
                calcDistanciaSegunRadio(nodoRaiz);
                i++;
            }
            jTextFieldRadio.setText(cant + "");
        }
        modelListaRespuestas = new DefaultComboBoxModel(listaNombre.toArray());
        jListListaRespuestas.setModel(modelListaRespuestas);
        jTextFieldCantidadDeCalculos.setText(cantidadCalculos + "");
        jLabelCantResp.setText(listaNombre.size() + "");
    }//GEN-LAST:event_jButtonCalcularDistanciaActionPerformed

    private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed
        try {
            nivelesPermitidos = Integer.parseInt(jTextFieldNivelesPermitidos.getText());
            nodoRaiz = crearArbol(ListaCadenas);
            DefaultTreeModel modeloArbol = new DefaultTreeModel(nodoRaiz);
            jTreeArbol.setModel(modeloArbol);
        } catch (java.lang.NumberFormatException e) {
        }

    }//GEN-LAST:event_jButtonResetActionPerformed

    private void jButtonSetRaizActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSetRaizActionPerformed
//        String raiz = jListCadenas.getSelectedValue().toString();
    }//GEN-LAST:event_jButtonSetRaizActionPerformed

    private void jComboBoxTipoBusquedaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxTipoBusquedaItemStateChanged
        if (jComboBoxTipoBusqueda.getSelectedItem() == "NN") {
            jTextFieldRadio.setEnabled(false);
        } else {
            jTextFieldRadio.setEnabled(true);
        }
    }//GEN-LAST:event_jComboBoxTipoBusquedaItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ArbolBKT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ArbolBKT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ArbolBKT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ArbolBKT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ArbolBKT().setVisible(true);
            }
        });
    }

    private DefaultMutableTreeNode crearArbol(ArrayList<String> listaTexto) {
        DefaultMutableTreeNode nodoPivote = getPivoteRandom(listaTexto);
        nodoPivote = getArbolDistancia(nodoPivote, listaTexto);

        ArrayList<DefaultMutableTreeNode> listaNodosHijosDePivote = getNodosHijos(nodoPivote);
        for (DefaultMutableTreeNode nodoDist : listaNodosHijosDePivote) {
            ArrayList<DefaultMutableTreeNode> listaNodosHijosDeDist = getNodosHijos(nodoDist);

            if (listaNodosHijosDeDist.size() >= Integer.parseInt(jTextFieldTamBucket.getText())) {
                alturaActual++;
                if (alturaActual < nivelesPermitidos) {
                    listaTexto = new ArrayList<String>();
                    for (DefaultMutableTreeNode nietos : listaNodosHijosDeDist) {
                        listaTexto.add(nietos.toString());
                    }
                    nodoDist.removeAllChildren();
                    nodoDist.add(crearArbol(listaTexto));
                }
                alturaActual--;
            }
        }
        return nodoPivote;
    }

    private DefaultMutableTreeNode getArbolDistancia(DefaultMutableTreeNode nodoPivote, ArrayList<String> listaTexto) {
        ArrayList<DefaultMutableTreeNode> nodosDistancia = getNodosDistancias(nodoPivote, listaTexto);
        for (DefaultMutableTreeNode defNodoDist : nodosDistancia) {
            nodoPivote.add(defNodoDist);
        }
        return nodoPivote;
    }

    private DefaultMutableTreeNode getPivoteRandom(ArrayList<String> listaTexto) {
        int rand = 0;
        if (jCheckBoxRandom.isSelected()) {
            rand = (int) Math.floor(Math.random() * listaTexto.size());
        }
        DefaultMutableTreeNode nodoPivote = new DefaultMutableTreeNode(listaTexto.get(rand));
        return nodoPivote;
    }

    /**
     * Retorna un nodo con la distancia y como hijos de este nodo las cadenas
     * con esa distancia.
     *
     * @param nodoPivote
     * @param lista
     * @return
     */
    private ArrayList<DefaultMutableTreeNode> getNodosDistancias(DefaultMutableTreeNode nodoPivote, ArrayList<String> lista) {
        Map<Integer, DefaultMutableTreeNode> mapa = new HashMap< Integer, DefaultMutableTreeNode>();
        for (int t = 0; t < lista.size(); t++) {
            String c2 = lista.get(t);
            int dist = LevenshteinDistance.computeLevenshteinDistance(nodoPivote.toString(), c2);

            DefaultMutableTreeNode nodoName = new DefaultMutableTreeNode(c2);
            DefaultMutableTreeNode nodoDist = null;

            if (mapa.containsKey(dist)) {
                nodoDist = mapa.get(dist);
            } else {
                nodoDist = new DefaultMutableTreeNode(dist);
            }
            nodoDist.add(nodoName);
            mapa.put(dist, nodoDist);
        }
        ArrayList<DefaultMutableTreeNode> listaDistancias = new ArrayList<DefaultMutableTreeNode>(mapa.values());
        return listaDistancias;
    }

    private ArrayList<DefaultMutableTreeNode> getNodosHijos(DefaultMutableTreeNode defNodoDist) {
        ArrayList<DefaultMutableTreeNode> lista = new ArrayList<DefaultMutableTreeNode>();
        for (int t = 0; t < defNodoDist.getChildCount(); t++) {
            DefaultMutableTreeNode hijos = (DefaultMutableTreeNode) defNodoDist.getChildAt(t);
            lista.add(hijos);
        }
        return lista;
    }

    private boolean esNodoHoja(DefaultMutableTreeNode nodo) {
        if (getNodosHijos(nodo).isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    private void calcDistanciaSegunRadio(DefaultMutableTreeNode RaizActual) {
        cantidadCalculos++;
        int dist = LevenshteinDistance.computeLevenshteinDistance(RaizActual.toString(), consulta);
        int rangoInf = dist - Integer.parseInt(jTextFieldRadio.getText().toString());
        int rangoSup = dist + Integer.parseInt(jTextFieldRadio.getText().toString());

        for (DefaultMutableTreeNode hijoDist : getNodosHijos(RaizActual)) {
            int disHijo = Integer.parseInt(hijoDist.toString());
            if (rangoInf <= disHijo && disHijo <= rangoSup) {
                for (DefaultMutableTreeNode hijoNombre : getNodosHijos(hijoDist)) {
                    if (!esNodoHoja(hijoNombre)) {
                        calcDistanciaSegunRadio(hijoNombre);
                    } else {
                        cantidadCalculos++;
                        dist = LevenshteinDistance.computeLevenshteinDistance(hijoNombre.toString(), consulta);
                        if (0 <= dist && dist <= Integer.parseInt(jTextFieldRadio.getText().toString())) {
                            listaNombre.add("d(" + hijoNombre.toString() + ")=" + dist);
                        }
                    }
                }
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupTipoArbol;
    private javax.swing.JButton jButtonCalcularDistancia;
    private javax.swing.JButton jButtonReset;
    private javax.swing.JButton jButtonSetRaiz;
    private javax.swing.JCheckBox jCheckBoxRandom;
    private javax.swing.JComboBox jComboBoxTipoBusqueda;
    private javax.swing.JFrame jFrameSelectRaiz;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabelCantResp;
    private javax.swing.JList jListCadenas;
    private javax.swing.JList jListListaRespuestas;
    private javax.swing.JRadioButton jRadioButtonBKT;
    private javax.swing.JRadioButton jRadioButtonFHQT;
    private javax.swing.JRadioButton jRadioButtonFQA;
    private javax.swing.JRadioButton jRadioButtonFQT;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextFieldCantidadDeCalculos;
    private javax.swing.JTextField jTextFieldConsulta;
    private javax.swing.JTextField jTextFieldNivelesPermitidos;
    private javax.swing.JTextField jTextFieldRadio;
    private javax.swing.JTextField jTextFieldTamBucket;
    private javax.swing.JTree jTreeArbol;
    // End of variables declaration//GEN-END:variables
}
